package cn.store.web.servlet;

import cn.store.domain.Category;
import cn.store.service.CategoryService;
import cn.store.service.serviceImp.CategoryServiceImp;
import cn.store.utils.JedisUtils;
import cn.store.utils.UUIDUtils;
import cn.store.web.base.BaseServlet;
import com.alibaba.fastjson.JSON;
import redis.clients.jedis.Jedis;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminCategoryServlet extends BaseServlet {
	//findAllCats
	public String findAllCats(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//获取全部分类信息
		CategoryService CategoryService=new CategoryServiceImp();
		List<Category> list=CategoryService.getAllCats();
		//全部分类信息放入request
		req.setAttribute("allCats", list);
		//转发到/admin/category/list.jsp
		return "/admin/category/list.jsp";
	}
	
	//addCategoryUI
	public String addCategoryUI(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		return "/admin/category/add.jsp";
	}
	//addCategory
	public String addCategory(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//获取分类名称
		String cname=req.getParameter("cname");
		//创建分类ID
		String id=UUIDUtils.getId();
		Category c=new Category();
		c.setCid(id);
		c.setCname(cname);
		//调用业务层添加分类功能
		CategoryService CategoryService=new CategoryServiceImp();
		CategoryService.addCategory(c);
		deleteRedis();//清空redis
		//重定向到查询全部分类信息
		resp.sendRedirect(req.getContextPath()+"/AdminCategoryServlet?method=findAllCats");
		return null;
	}
	/*
	 * 通过Cid查询商品类目
	 * */
	public String getCatById(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		/*获得商品类别id*/
		String cid = req.getParameter("cid");
		CategoryService categoryService=new CategoryServiceImp();

		Category category = categoryService.getCatById(cid);
		/*清空redis的数据*/
		deleteRedis();
		req.setAttribute("category",category);
		return "/admin/category/edit.jsp";
	}

	//更新
	public String updateCategory(HttpServletRequest req, HttpServletResponse resp) throws Exception {
		//获取分类名称
		String cname=req.getParameter("cname");
		//创建分类ID
		String id=req.getParameter("cid");
		Category c=new Category();
		c.setCid(id);
		c.setCname(cname);
		//调用业务层添加分类功能
		CategoryService CategoryService=new CategoryServiceImp();
		CategoryService.updateCategory(c);
		deleteRedis();
		//重定向到查询全部分类信息
		resp.sendRedirect(req.getContextPath()+"/AdminCategoryServlet?method=findAllCats");
		return null;
	}

	/*
	 * 通过Cid刪除商品类目
	 * */
	public String deleteCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
		String cid = req.getParameter("cid");
		Map<String,String> msg = new HashMap<>();
		resp.setContentType("application/json; charset=utf-8");
		PrintWriter p =resp.getWriter();
		try {
			/*尝试删除*/
			CategoryService categoryService=new CategoryServiceImp();
			categoryService.deleteCategory(cid);
			msg.put("flag","true");
			deleteRedis();
		}catch (Exception e){
			/*删除失败*/
			msg.put("flag","false");
			e.printStackTrace();
		}finally {
			String jsonStr = JSON.toJSONString(msg);
			System.out.println(jsonStr);
			p.write(jsonStr);
			return null;
		}

	}
	public void deleteRedis(){
		Jedis jedis;
		try{
			jedis= JedisUtils.getJedis();
			String jsonStr=jedis.get("allCats");
			if(jsonStr != null ){
				jedis.del("allCats");
			}
		}catch (Exception e){
			e.printStackTrace();
		}
	}
}
